﻿using System;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CountryInfoDB
/// </summary>
/// 
namespace CE.AreaCountry
{
    public class CountryInfoDB
    {
        public CountryInfoDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CountryInfo countryinfo)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "sprocCountryInfoInsertUpdateSingleItem";

            SortedList _sPara = new SortedList();

            #region Parameter

            _sPara.Add("@CountryID", countryinfo.CountryId);
            _sPara.Add("@Description", countryinfo.Description);
            _sPara.Add("@Status", countryinfo.Status);

            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public CountryInfo GetCountryInfo(Int64 COId)
        {
            CountryInfo _countryinfo = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocCountryInfoSelectItemList";

            SortedList _spara = new SortedList();
            _spara.Add("@CountryID", COId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _countryinfo = new CountryInfo();
                    _countryinfo = FillDataRecord(_dt, 0);
                }
            }
            return _countryinfo;
        }

        public DataTable GetCountryInfoList()
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Countries");
            string _spName = "sprocCountryInfoSelectItemList";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CountryID", null);
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }


        public DataTable SearchCountryInfo(string searchparam)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Countries");
            string _spName = "sprocCountryInfoSearchItemList";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@search", searchparam);           
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        public DataTable GetCountryInfoByDescription(string desc)
        {
            MyDatabase _db = new MyDatabase();
            DataTable _dtResult = new DataTable("Countries");
            string _spName = "sprocCountryInfoSelectItemByDescription";
            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@Country", desc);
            #endregion

            _dtResult = _db.GetDataTableBySP(_spName, _sPara);
            return _dtResult;
        }

        private CountryInfo FillDataRecord(DataTable myDataTable, int rowId)
        {
            CountryInfo myCountryInfo = new CountryInfo();
            myCountryInfo.CountryId = Convert.ToInt64(myDataTable.Rows[rowId]["CountryId"].ToString());
            myCountryInfo.Description = myDataTable.Rows[rowId]["Description"].ToString();
            myCountryInfo.Status = Convert.ToBoolean(myDataTable.Rows[rowId]["Status"].ToString());

            return myCountryInfo;
        }
    }
}